//
//  ViewController.m
//  WXPayDemo
//
//  Created by yifutong on 2018/12/14.
//  Copyright © 2018年 yifutong. All rights reserved.
//

#import "ViewController.h"
#import "WXPayGetJson.h"
#import <CoreLocation/CoreLocation.h>
#import <wxPayFramework/WXAppletAPI.h>
@interface ViewController () <CLLocationManagerDelegate,WXPayGetJsonDelegate>
@property (nonatomic,strong) NSDictionary * positioning;
@property (nonatomic,assign) NSInteger index;
@end

@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    _positioning = [[NSDictionary alloc] init];
    UIButton * button = [UIButton buttonWithType:UIButtonTypeCustom];
    [button setFrame:CGRectMake(0, 100, [UIScreen mainScreen].bounds.size.width, 50)];
    [button setTitle:@"唤醒微信小程序(不使用sdk)" forState:UIControlStateNormal];
    [button setBackgroundColor:[UIColor grayColor]];
    [button setTag:1001];
    [button setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
    [button addTarget:self action:@selector(btnClick:) forControlEvents:UIControlEventTouchUpInside];
    [self.view addSubview:button];
    
    UIButton * button1 = [UIButton buttonWithType:UIButtonTypeCustom];
    [button1 setFrame:CGRectMake(0, 160,  [UIScreen mainScreen].bounds.size.width, 50)];
    [button1 setTitle:@"唤醒微信小程序(调用sdk)" forState:UIControlStateNormal];
    [button1 setTag:1002];
    [button1 setBackgroundColor:[UIColor grayColor]];
    [button1 setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
    [button1 addTarget:self action:@selector(btnClick:) forControlEvents:UIControlEventTouchUpInside];
    [self.view addSubview:button1];
}
- (void)btnClick:(UIButton *)btn{
    self.index = btn.tag;

    WXPayGetJson * api = [[WXPayGetJson alloc] init];
    api.delegate = self;
    [api getWXAPPInfo];
}

#pragma mark -- WXPayGetAPIDelegate
- (void)getResponseData:(NSDictionary *)dict {
    NSLog(@"dict === %@",dict);
    if (self.index == 1001) {
        //不使用SDK调起小程序支付
        if ([self wXXCx:dict]) {
            NSLog(@"成功");
        }else {
            NSLog(@"失败");
        }
    }else {
        //使用SDK ---调起微信小程序支付
        //第一个参数不填默认展示名称为还款宝
        [WXAppletAPI smallProgramJump:@"测试" dict:dict];
    }
}
- (BOOL)wXXCx:(NSDictionary *)dict {
    WXLaunchMiniProgramReq *launchMiniProgramReq = [WXLaunchMiniProgramReq object];
    [WXApi registerApp: dict[@"json"][@"appid"]]; // 这个也可配置绑定过小程序的appid
    launchMiniProgramReq.userName = dict[@"json"][@"gid"];  //拉起的小程序的username
     //拉起小程序页面的可带参路径，不填默认拉起小程序首页 productName 默认还款宝
    launchMiniProgramReq.path = [NSString stringWithFormat:@"pages/pay/pay?json=%@",dict[@"json"][@"requestParam"]];
    //设置展示值
//    launchMiniProgramReq.path = [NSString stringWithFormat:@"pages/pay/pay?json=%@&productName=%@",dict[@"json"][@"requestParam"],@"测试"];

    launchMiniProgramReq.extMsg = @"";
    launchMiniProgramReq.miniProgramType = WXMiniProgramTypeRelease; //分享小程序的版本（正式，开发，体验）
    return [WXApi sendReq:launchMiniProgramReq];
}

@end
